package game.module.chat.logic;

import game.entity.Hero;
import game.module.badwords.dao.BadWordsFilter;
import game.module.chat.ChatConstants;
import game.module.chat.response.ChatAllMsg30114;
import game.module.chat.response.ChatFailMsg30120;
import game.serverenv.GameServerOnlineManager;

import java.util.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ChatManager {

	private static Logger logger = LoggerFactory.getLogger(ChatManager.class);

	@Autowired
	private GameServerOnlineManager gameServerOnlineManager;

	@Autowired
	private BadWordsFilter badWordsFilter;

	public void sendChatMsg(Hero hero, String content, int type) {
		switch (type) {
		case ChatConstants.MSG_CHAT_TEAM:
			sendTeamMsg(hero, content);
			break;
		case ChatConstants.MSG_CHAT_SCENE:
			sendScenemMsg(hero, content);
			break;
		case ChatConstants.MSG_CHAT_FACTION:
			sendFactionMsg(hero, content);
			break;
		case ChatConstants.MSG_CHAT_WORLD:
			sendWorldMsg(hero, content);
			break;
		case ChatConstants.MSG_CHAT_LABA:
			sendLabaMsg(hero, content);
			break;
		default:
			logger.error("err chat msg Type "
					+ type
					+ " right type is teammsg==ChatManager.MSG_CHAT_TEAM=1; scenemsg==MSG_CHAT_SCENE=2; bangmsg==MSG_CHAT_FACTION=3;worldmsg==MSG_CHAT_WORLD=4;labamsg==MSG_CHAT_LABA=5");
			break;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("[type=" + type + "]" + " msg " + content);
		}
	}

	private void sendLabaMsg(Hero hero, String content) {

	}

	private void sendWorldMsg(Hero chatRole, String content) {
		byte CHANNEL = 5;
		if (chatRole.getGrade() < 20) {
			chatRole.sendMsg(new ChatFailMsg30120(CHANNEL, 13014));
			return;
		}
		content = badWordsFilter.badWordstFilterStr(content);
		// 内容过滤
		boolean isGm = false;
		sendWorldMsgExceptBlackFriend(new ChatAllMsg30114(chatRole, content, isGm));
	}

	/**
	 * 发送世界聊天消息
	 * 
	 * @param chatAllMsg30114
	 */
	private void sendWorldMsgExceptBlackFriend(ChatAllMsg30114 chatAllMsg30114) {
		Queue<Hero> heroList = gameServerOnlineManager.getCharacterList();
		for (Hero hero : heroList) {
			hero.sendMsg(chatAllMsg30114);
		}
	}

	private void sendFactionMsg(Hero hero, String content) {

	}

	private void sendScenemMsg(Hero hero, String content) {

	}

	private void sendTeamMsg(Hero hero, String content) {

	}

}
